<template>
  <div class='container'>
    <el-card class="box-card">
      <span class="title">说明: 目前只有学科和关键字条件筛选</span>
      <el-button type="success" icon="el-icon-edit" size="small" class="right" @click="testEntr">新增试题</el-button>
      <el-form ref="form" v-model="queryInfo">
        <el-row>
            <el-col :span="6">
              <el-form-item label="学科" label-width="80px">
                <el-select style="width: 100%" v-model="queryInfo.subjectID" placeholder="请选择" @change="secondaryDirectory(queryInfo.subjectID)">
                  <el-option v-for="(item,index) in simpleList" :key='index' :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录" label-width="80px">
                <el-select style="width: 100%" v-model="queryInfo.catalogID" placeholder="请选择" @change="getAble(queryInfo.subjectID)">
                  <el-option v-for="item in second" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签" label-width="80px">
                <el-select style="width: 100%" v-model="queryInfo.tagName" placeholder="请选择">
                  <el-option :label="item.label" :value="item.value" v-for="item in lableList" :key="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字" label-width="80px">
                <el-input  style="width: 100%" v-model="queryInfo.keyword" placeholder="根据题干搜索"></el-input>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="试题类型" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.questionType" placeholder="请选择">
                <el-option :label="item.label" :value="item.value" v-for="item in questionType" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.difficulty" placeholder="请选择">
                <el-option :label="item.label" :value="item.value" v-for="item in difficulty" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.direction" placeholder="请选择">
                <el-option :label="item" :value="index" v-for="(item, index) in direction" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人" label-width="80px">
              <el-select style="width: 100%" v-model="queryInfo.creatorID" placeholder="请选择">
                <el-option :label="item.username" :value="item.id" v-for="item in CreatorIdList" :key="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="6">
            <el-form-item label="题目备注" label-width="80px" v-model="queryInfo.remarks">
              <el-input style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称" label-width="80px" v-model="queryInfo.shortName">
              <el-input style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市" label-width="80px">
              <el-select style="width: 40%" v-model="queryInfo.provinces" placeholder="请选择" @change="City(queryInfo.provinces)">
                <el-option :label="item" :value="item" v-for="(item, index) in provinces" :key="index"></el-option>
              </el-select>
              <el-select style="width: 50%" v-model="queryInfo.citys" placeholder="请选择">
                <el-option :label="item" :value="index" v-for="(item, index) in citys" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="80px">
              <el-button size="mini" @click="del">取消</el-button>
              <el-button type="primary" size="mini" @click="search">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
<!--     提示文本-->
      <div>
        <el-alert
          type="info"
          show-icon>
          <span slot="title">数据一共{{total}}条</span>
        </el-alert>
      </div>
<!--      数据表格-->
      <el-table
        :data="basicsList"
        style="width: 100%">
        <el-table-column
          prop="number"
          label="试题编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="subject"
          label="学科"
          width="180">
        </el-table-column>
        <el-table-column
          prop="catalog"
          label="目录">
        </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型">
          <template slot-scope="scope">
            {{question(scope.row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="question"
          label="题干">
          <template slot-scope="scope">
            <div v-html="scope.row.question"></div>
          </template>
        </el-table-column>
        <el-table-column
          prop="addDate"
          label="录入时间">
        </el-table-column>
        <el-table-column
          label="难度">
          <template slot-scope="scope">
            {{diff(scope.row.difficulty)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人">
        </el-table-column>
        <el-table-column
          width="200"
          label="操作">
           <template slot-scope="scope">
             <el-button icon="el-icon-view" circle size="mini" plain @click="preview(scope.row.id)"></el-button>
             <el-button type="primary" icon="el-icon-edit" circle size="mini" plain @click="testEntry(scope.row.id)"></el-button>
             <el-button type="danger" icon="el-icon-delete" circle size="mini" plain @click="delList(scope.row.id)"></el-button>
             <el-button type="success" icon="el-icon-check" circle size="mini" plain @click="choose(scope.row)"></el-button>
           </template>
        </el-table-column>
      </el-table>
<!--      分页功能-->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <!--题目预览区域  -->
    <el-dialog
      title="题目预览"
      :visible.sync="diaItenVisible"
      width="50%"
      v-if="diaItenVisible"
    >
      <questions-preview :id="rowId"></questions-preview>
      <span slot="footer" class="dialog-footer">
          <el-button @click="diaItenVisible = false" type="primary"
          >关闭</el-button
          >
        </span>
    </el-dialog>
  </div>
</template>
<script>
import { list, remove, choiceAdd } from '@/api/hmmm/questions'
import { simple } from '@/api/hmmm/subjects'
import { simple as direct } from '@/api/hmmm/directorys'
import { simple as lable } from '@/api/hmmm/tags'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { simple as people } from '@/api/base/users'
import { citys, provinces } from '@/api/hmmm/citys'
import QuestionsPreview from '@/module-hmmm/components/questions-preview'

export default {
  data () {
    return {
      form: {
        page: 1,
        pagesize: 5
      },
      // 题型
      questionType,
      // 难度
      difficulty,
      // 方向
      direction,
      // 城市
      citys: [],
      provinces: provinces(),
      queryInfo: {
        page: 1,
        pagesize: 5
        // // 选中的下拉框的值
        // subjectID: '',
        // // 选中的二级目录
        // catalogID: '',
        // // 选中的标签
        // tags: ''
        // 题型
        // questionType: '',
        // // 难度
        // difficulty: '',
        // // 方向
        // direction: '',
        // // 录入人
        // creatorID: '',
        // citys: '',
        // provinces: '',
        // // 关键字
        // // keyword: '',
        // // 题目备注
        // remarks: '',
        // // 企业简称
        // shortName: ''
      },
      total: 0,
      // 基础题库列表
      basicsList: [],
      // 简单的学科列表
      simpleList: [],
      // 二级目录
      second: [],
      // 标签列表
      lableList: [],
      // 录入人信息
      CreatorIdList: [],
      // 弹出框的显示还是隐藏
      diaItenVisible: false,
      // 每一条数据的id
      rowId: ''
    }
  },
  components: {
    QuestionsPreview
  },
  created () {
    this.getList()
    this.getSleepList()
    this.getCreatorID()
  },
  methods: {
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.getList()
    },
    // 获取基础题库的列表
    async getList () {
      try {
        const { data: res } = await list(this.queryInfo)
        this.basicsList = res.items
        console.log(this.basicsList)
        this.total = res.counts
      } catch (err) {}
    },
    // 获取简单的学科列表
    async getSleepList () {
      try {
        const { data: res } = await simple()
        this.simpleList = res
      } catch (err) {}
    },
    async secondaryDirectory (id) {
      // 获取二级目录
      const { data: res } = await direct({ subjectID: id })
      this.second = res
      this.queryInfo.catalogID = ''
      this.queryInfo.tags = ''
    },
    // 获取标签列表
    async getAble (id) {
      const { data: res } = await lable({ subjectID: id })
      this.lableList = res
    },
    // 获取录入人信息
    async getCreatorID () {
      try {
        const { data: res } = await people()
        this.CreatorIdList = res
      } catch (err) {}
    },
    City (id) {
      console.log(id)
      const cityList = citys(id)
      this.citys = cityList
    },
    // 点击取消让表单内容清空
    del () {
      this.queryInfo = {}
    },
    // 点击搜索
    async search () {
      const { data: res } = await list(this.queryInfo)
      this.basicsList = res.items
      this.total = res.counts
    },
    // 点击删除
    delList (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((err) => {
        console.dir(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 精选
    choose (data) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        await choiceAdd({ id: data.id, choiceState: 1 })
        this.getList()
        this.$message({
          type: 'success',
          message: '加入精选成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消精选'
        })
      })
    },
    // 点击编辑
    testEntry (id) {
      this.$router.push({ path: 'new', query: { id: id } })
    },
    // 点击新增试题
    testEntr () {
      this.$router.push({ path: 'new' })
    },
    // 点击预览
    preview (id) {
      this.rowId = id
      this.diaItenVisible = true
    }
  },
  computed: {
    // 判断难度
    diff: function () {
      return function (val) {
        if (val === '1') {
          return '简单'
        } else {
          return '困难'
        }
      }
    },
    // 判断题型
    question: function () {
      return function (val) {
        if (val === '1') {
          return '单选'
        } else if (val === '2') {
          return '多选'
        } else {
          return '简答'
        }
      }
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  padding: 10px;
}
.title {
  font-size: 12px;
  color: red;
  opacity:0.3;
  display: inline-block;
  margin-bottom: 40px;
}
.right {
  float: right;
}
</style>
